package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_match_prize;

import cn.com.api.service.ApiU_match_prizeService;

import cn.com.util.*;


public class U_match_prizeBaseController {

	private static Logger logger = Logger.getLogger(U_match_prizeBaseController.class);
	
	@Autowired
	private ApiU_match_prizeService apiU_match_prizeService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_match_prize-list")
	public ModelAndView u_match_prize_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_match_prize_list.jsp");
		
		List<ApiU_match_prize> u_match_prizes = apiU_match_prizeService.findU_match_prizes();
		mv.addObject("u_match_prizes", u_match_prizes);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_match_prize-add-pre")
	public ModelAndView u_match_prize_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_match_prize_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_match_prize u_match_prize = apiU_match_prizeService.findU_match_prizeById(Integer.valueOf(id));
			mv.addObject("u_match_prize",u_match_prize);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_match_prize-add")
	public void u_match_prize_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String prize_id = request.getParameter("prize_id");
		String match_id = request.getParameter("match_id");
		String prize_name = request.getParameter("prize_name");
		String prize_num = request.getParameter("prize_num");
		String prize_money = request.getParameter("prize_money");
		String winner_count = request.getParameter("winner_count");
		String prize_percent = request.getParameter("prize_percent");
		String remark = request.getParameter("remark");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_match_prize u_match_prize = new ApiU_match_prize();
            /*
		    u_match_prize.setPrize_id(prize_id);
		    u_match_prize.setMatch_id(match_id);
		    u_match_prize.setPrize_name(prize_name);
		    u_match_prize.setPrize_num(prize_num);
		    u_match_prize.setPrize_money(prize_money);
		    u_match_prize.setWinner_count(winner_count);
		    u_match_prize.setPrize_percent(prize_percent);
		    u_match_prize.setRemark(remark);
		    u_match_prize.setExt1(ext1);
		    u_match_prize.setExt2(ext2);
		    u_match_prize.setExt3(ext3);
	        */
			apiU_match_prizeService.insertU_match_prize(u_match_prize);
		}
		//edit
		else{
			ApiU_match_prize u_match_prize = apiU_match_prizeService.findU_match_prizeById(Integer.valueOf(id));
	        /*
		    u_match_prize.setPrize_id(prize_id);
		    u_match_prize.setMatch_id(match_id);
		    u_match_prize.setPrize_name(prize_name);
		    u_match_prize.setPrize_num(prize_num);
		    u_match_prize.setPrize_money(prize_money);
		    u_match_prize.setWinner_count(winner_count);
		    u_match_prize.setPrize_percent(prize_percent);
		    u_match_prize.setRemark(remark);
		    u_match_prize.setExt1(ext1);
		    u_match_prize.setExt2(ext2);
		    u_match_prize.setExt3(ext3);
			*/
			apiU_match_prizeService.updateU_match_prize(u_match_prize);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_match_prize-delete")
	public void u_match_prize_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_match_prizeService.deleteU_match_prize(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



